package com.ruoyi.bill.controller;

import java.util.*;
import java.util.concurrent.TimeUnit;

import com.ruoyi.bill.domain.vo.BillFinancePortVo;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.dataMain.listener.BillPortImportListener;
import com.ruoyi.dataMain.listener.BillfinancePortImportListener;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.bill.domain.vo.BillPortVo;
import com.ruoyi.bill.domain.bo.BillPortBo;
import com.ruoyi.bill.service.IBillPortService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 端口台账
 *
 * @author linziheng
 * @date 2024-02-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/bill/port")
public class BillPortController extends BaseController {

    private final IBillPortService iBillPortService;


    /**
     * 手动匹配按钮
     * @return
     */
    @GetMapping("/matching")
    public R matching() {
        return iBillPortService.matching();
    }

    /**
     *  端口台账total面板数据
     * @param now
     * @return
     */
    @GetMapping("/getTodayTotal")
    public Map getTodayTotal(String now) {
        return iBillPortService.getTodayTotal(now);
    }

    @GetMapping("/accountBelongList")
    public List<String> accountBelongList() {
        return iBillPortService.accountBelongList();
    }
    /**
     * 导入数据
     *
     * @param file          导入文件
     */
    @Log(title = "开户导入", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<BillPortVo> result = ExcelUtil.importExcel(file.getInputStream(), BillPortVo.class, new BillPortImportListener());
        return R.ok(result.getAnalysis());
    }


    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "端口台账", BillPortVo.class, response);
    }
    /**
     * 查询端口台账列表
     */
    @SaCheckPermission("bill:port:list")
    @GetMapping("/list")
    public TableDataInfo<BillPortVo> list(BillPortBo bo, PageQuery pageQuery) {
        return iBillPortService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出端口台账列表
     */
    @SaCheckPermission("bill:port:export")
    @Log(title = "端口台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BillPortBo bo, HttpServletResponse response) {
        List<BillPortVo> list = iBillPortService.queryList(bo);
        ExcelUtil.exportExcel(list, "端口台账", BillPortVo.class, response);
    }

    /**
     * 获取端口台账详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("bill:port:query")
    @GetMapping("/{id}")
    public R<BillPortVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iBillPortService.queryById(id));
    }

    /**
     * 新增端口台账
     */
    @SaCheckPermission("bill:port:add")
    @Log(title = "端口台账", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BillPortBo bo) {
        return toAjax(iBillPortService.insertByBo(bo));
    }


    /**
     * 补录端口台账
     */
    @SaCheckPermission("bill:port:add")
    @Log(title = "端口台账", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/record")
    public R<Void> record(@Validated(AddGroup.class) @RequestBody BillPortBo bo) {
        return toAjax(iBillPortService.recordByBo(bo));
    }

    /**
     * 修改端口台账
     */
    @SaCheckPermission("bill:port:edit")
    @Log(title = "端口台账", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BillPortBo bo) {
        return toAjax(iBillPortService.updateByBo(bo));
    }

    /**
     * 删除端口台账
     *
     * @param ids 主键串
     */
    @SaCheckPermission("bill:port:remove")
    @Log(title = "端口台账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iBillPortService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
